package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.view.BaseItemView;

/**
 * Created by liqp on 2016/6/15.
 */
public class SceneLampAdapter extends AppArrayAdapter<Device, SceneLampAdapter.SceneLampViewHolder> {

    public SceneLampAdapter(Context context){
        super(context, R.layout.lamp_view_item, SceneLampViewHolder.class);
    }

    public View bindHolder(View convertView, ViewGroup parent, final int position) {
        final Device lamp = this.devices.get(position);
        final SceneLampViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new SceneLampViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.lamp_view_item, parent, false);
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 2) - 1, (int) (parent.getHeight() / 2)));// 动态设置item的高度
            viewHolder.tv_lamp_name = (TextView) convertView.findViewById(R.id.tv_lamp_desc);
            viewHolder.tv_lamp_status = (BaseItemView) convertView.findViewById(R.id.baseItemView_lamp);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (SceneLampViewHolder) convertView.getTag();
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 2) - 1, (int) (parent.getHeight() / 2)));// 动态设置item的高度
        }
        viewHolder.tv_lamp_name.setText(lamp.getDeviceName());
        viewHolder.tv_lamp_status.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if ( Integer.parseInt(lamp.getequipNo().split("-")[0]) == box1) {
                        if (lamppower.get(0)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 0) {
                            lamppower.get(0)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] = 1;
                            viewHolder.tv_lamp_status.turnOn();

                        } else if (lamppower.get(0)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 1) {
                            lamppower.get(0)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] = 0;
                            viewHolder.tv_lamp_status.turnOff();

                        }
                    }else if( Integer.parseInt(lamp.getequipNo().split("-")[0]) == box2){
                        if (lamppower.get(1)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 0) {
                            lamppower.get(1)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] = 1;
                            viewHolder.tv_lamp_status.turnOn();

                        } else if (lamppower.get(1)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 1) {
                            lamppower.get(1)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] = 0;
                            viewHolder.tv_lamp_status.turnOff();
                        }
                    }
                    notifyDataSetChanged();
                }
                return false;
            }
        });

        if (lamppower != null){
            if ( Integer.parseInt(lamp.getequipNo().split("-")[0]) == box1) {
                if (lamppower.get(0)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 0) {

                    viewHolder.tv_lamp_status.turnOff();

                } else if (lamppower.get(0)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 1) {
                    viewHolder.tv_lamp_status.turnOn();

                }
            }else if( Integer.parseInt(lamp.getequipNo().split("-")[0]) == box2) {
                if (lamppower.get(1)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 0) {
                    viewHolder.tv_lamp_status.turnOff();
                } else if (lamppower.get(1)[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] == 1) {
                    viewHolder.tv_lamp_status.turnOn();
                }
            }
        }

        return convertView;
    }

    public class SceneLampViewHolder extends AppHolder {
        public BaseItemView tv_lamp_status;
        public TextView tv_lamp_name;
    }
}
